/*
 * Serum
 *
 * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
 */

Serum.Form.Module.Fields.UploadPreview = Ext.extend(Ext.Panel, {

    /**
     * Constructor
     * 
     * @param   {Object}    baseConfig
     */
    constructor: function(baseConfig) {
        var config  = Ext.apply({
            fieldLabel: baseConfig.fieldLabel || __('base~form_label_upload')
        }, baseConfig);

        var _items = [];
        this.uploadField = new Ext.form.TextField({
                                    fieldLabel:     config.fieldLabel,
                                    inputType:      'file',
                                    name:           'file-' + config.name
                                });
        _items.push(this.uploadField);

        if (config.preview !== '') {
            var deleteBtn = new Ext.Button({
                text: __('base~form_delete'),

                listeners: {
                    click: function(btn) {
                        // Ajax request to get fields list
                        Serum.Ajax.request({
                            scope:       this,
                            params:     {
                                            method:         Serum.Selector.get().method({
                                                                                        action: 'deleteFile'
                                                                                    }),
                                            requestId:      Serum.Helper.Request.id(),
                                            elementId:      3,
                                            attributeName:  config.name,
                                            attributeValue: config.value
                                        },
                            onSuccess:  function(response) {
                                            this.remove(btn);
                                            this.remove(this.findById('img-preview-' + config.fieldLabel));
                                        }
                        });


                    },
                    scope: this
                }
             });

             //_items.push(deleteBtn);

            _items.push(new Ext.form.Field({
                            id: 'img-preview-' + config.fieldLabel,
                            fieldLabel: __('base~form_preview'),
                            autoEl: {
                                tag:    'img',
                                src:    config.preview + '?reload=' + Serum.Helper.Request.id(),
                                alt:    config.fieldLabel,
                                width: '30%'
                            },
                            value: config.preview
                         }));
        }

        Serum.Form.Module.Fields.UploadPreview.superclass.constructor.call(this, Ext.apply({
            border: false,
            layout: 'form',
            items: _items
        }, config));  
    }
});

Ext.reg('uploadpreviewfield', Serum.Form.Module.Fields.UploadPreview);